package com.gdut.huayan.service.impl;

import com.gdut.huayan.dao.ActivityMapper;
import com.gdut.huayan.dao.CharityMapper;
import com.gdut.huayan.dao.NameMapper;
import com.gdut.huayan.dao.TabletMapper;
import com.gdut.huayan.entity.po.Name;
import com.gdut.huayan.entity.po.Tablet;
import com.gdut.huayan.service.PrintService;
import com.gdut.huayan.service.exception.BaseException;
import com.gdut.huayan.utils.StringUtils;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class PrintServiceImpl implements PrintService {

    private final Integer WANGSHENG = 1;
    private final Integer YANSHENG = 2;
    private final Integer WANGSHENG_LIDAIXIANZU = 1;
    private final Integer WANGSHENG_XIANREN = 2;
    private final Integer WANGSHENG_SHIFANG = 3;
    private final Integer WANGSHENG_DIJIZHU = 4;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private TabletMapper tabletMapper;

    @Autowired
    private CharityMapper charityMapper;

    @Autowired
    private NameMapper nameMapper;

    @Override
    public List<Tablet> getPaidTabletByDetailId(Long festivalId, Long locationId, Long activityId, Integer printType) {
        return tabletMapper.getPaidTablet(festivalId, locationId, activityId, printType);
    }

    private Tablet checkTabletInfo(List<Tablet> tabletList) {
        boolean flag = false;
        Tablet flagTablet = tabletList.get(0);
        for (Tablet tablet : tabletList) {
            if (!Objects.equals(tablet.getTabletType(), flagTablet.getTabletType())) {
                flag = true;
            }
            if (tablet.getTabletType() == 1 && !Objects.equals(tablet.getLastTabletType(), flagTablet.getLastTabletType())) {
                flag = true;
            }
            if (!Objects.equals(tablet.getSizeType(), flagTablet.getSizeType())) {
                flag = true;
            }
            if (!Objects.equals(tablet.getPrintType(), flagTablet.getPrintType())) {
                flag = true;
            }
        }
        if (flag) {
            throw new BaseException(400, "所传入牌位类型不一致，无法打印");
        }
        return flagTablet;
    }

    @Override
    public void  getTabletPaperByIdsV2(List<Long> ids, HttpServletResponse response) throws IOException {
        List<Tablet> tabletList = tabletMapper.selectBatchIds(ids);
        tabletList.forEach(Tablet::getTabletNumber);
        int sum = tabletList.size();
        Tablet flagTablet;
        if (sum > 0) {
            flagTablet = checkTabletInfo(tabletList);
        } else {
            throw new BaseException(400, "暂无相关id的牌位信息");
        }
        response.setContentType("application/octet-stream;charset=utf-8");
        PdfFont font = PdfFontFactory.createFont("pdf_template/STKAITI.TTF", "Identity-H");
        PdfDocument originPdf = null, resultPdf = new PdfDocument(new PdfWriter(response.getOutputStream()));
        switch (flagTablet.getSizeType()) {
            case 1: {
                // 大排位
                if (flagTablet.getPrintType() == 1) {
                    //个人
                    if (WANGSHENG.equals(flagTablet.getTabletType())) {
                        if (WANGSHENG_LIDAIXIANZU.equals(flagTablet.getLastTabletType()) || WANGSHENG_XIANREN.equals(flagTablet.getLastTabletType())) {
                            originPdf = new PdfDocument(new PdfReader("pdf_new_template/big/往生牌位-历代祖先-大牌位.pdf"));
                            for (int i = 0; i < sum; i++) {
                                originPdf.copyPagesTo(1, 1, resultPdf);
                            }
                            int page = 1;
                            for (Tablet tablet : tabletList) {
                                List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                                PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                                //祖先姓
                                zuXianXinFill(canvas, font, tablet.getAncestralSurname(), 35, 390, 780);
                                for (int i = 0; i < names.size(); i++) {
                                    switch (names.get(i).getType()) {
                                        case 1: {
                                            ancestralFill(canvas, font, names.get(i).getText().split("@"), 24, 460, 740);
                                            break;
                                        }
                                        case 2: {
                                            personYangShangFill(names.get(i).getText().split("@"), canvas, font,
                                                    343,
                                                    495, 19,
                                                    310,
                                                    510,
                                                    310,
                                                    430,
                                                    30,
                                                    20);
                                            break;
                                        }
                                    }
                                }

                                //TODO 编号部分
                            }


                        } else if (WANGSHENG_SHIFANG.equals(flagTablet.getLastTabletType())) {
                            originPdf = new PdfDocument(new PdfReader("pdf_new_template/big/往生牌位-十方法界-大牌位.pdf"));
                            for (int i = 0; i < sum; i++) {
                                originPdf.copyPagesTo(1, 1, resultPdf);
                            }
                            int page = 1;
                            for (Tablet tablet : tabletList) {
                                List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                                PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                                for (int i = 0; i < names.size(); i++) {
                                    if (names.get(i).getType() == 2) {
                                        personYangShangFill(names.get(i).getText().split("@"), canvas, font,
                                                337,
                                                505,
                                                19,
                                                310,
                                                515,
                                                310,
                                                440,
                                                30,
                                                20);
                                        break;
                                    }
                                }
                                //TODO 编号
                                //numberFill();
                            }
                        } else if (WANGSHENG_DIJIZHU.equals(flagTablet.getLastTabletType())) {
                            originPdf = new PdfDocument(new PdfReader("pdf_new_template/big/往生牌位-地基主-大牌位.pdf"));
                            for (int i = 0; i < sum; i++) {
                                originPdf.copyPagesTo(1, 1, resultPdf);
                            }

                            int page = 1;
                            for (Tablet tablet : tabletList) {
                                List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                                PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));

                                for (int i = 0; i < names.size(); i++) {
                                    switch (names.get(i).getType()) {
                                        case 2: {
                                            personYangShangFill(names.get(i).getText().split("@"), canvas, font,
                                                    330,
                                                    497,
                                                    23,
                                                    300,
                                                    510,
                                                    300,
                                                    430,
                                                    30,
                                                    20);
                                            break;
                                        }
                                        case 4: {
                                            // 地基主
                                            dijizhuFill(canvas, font, names.get(i).getText().split("@"), 425, 775, 400.0f, 390);
                                            break;
                                        }
                                    }
                                }
                            }

                        }
                    } else {
                        originPdf = new PdfDocument(new PdfReader("pdf_new_template/big/延生牌位-大牌位.pdf"));
                        for (int i = 0; i < sum; i++) {
                            originPdf.copyPagesTo(1, 1, resultPdf);
                        }
                        int page = 1;
                        for (Tablet tablet : tabletList) {
                            List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                            PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                            for (int i = 0; i < names.size(); i++) {
                                if (names.get(i).getType() == 3) {
                                    personYangShangFill(names.get(i).getText().split("@"), canvas, font,
                                            405,
                                            750,
                                            23,
                                            381,
                                            770,
                                            364,
                                            690,
                                            30,
                                            20);
                                    break;
                                }
                            }
                        }

                        //TODO 编号部分
                        //numberFill(canvas,font,tablet.getId(),255,375);
                    }
                } else {
                    if (WANGSHENG.equals(flagTablet.getTabletType())) {
                        if (WANGSHENG_SHIFANG.equals(flagTablet.getLastTabletType())) {
                            originPdf = new PdfDocument(new PdfReader("pdf_new_template/big/往生牌位-十方法界-企业-大牌位.pdf"));
                            for (int i = 0; i < sum; i++) {
                                originPdf.copyPagesTo(1, 1, resultPdf);
                            }
                            int page = 1;
                            for (Tablet tablet : tabletList) {
                                List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                                PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                                for (int i = 0; i < names.size(); i++) {
                                    if (names.get(i).getType() == 2) {
                                        //TODO sc说先不管
                                        break;
                                    }
                                }
                                //TODO 编号
                                //numberFill();
                            }
                        } else if (WANGSHENG_DIJIZHU.equals(flagTablet.getLastTabletType())) {
                            originPdf = new PdfDocument(new PdfReader("pdf_new_template/big/往生牌位-地基主-大牌位.pdf"));
                            for (int i = 0; i < sum; i++) {
                                originPdf.copyPagesTo(1, 1, resultPdf);
                            }

                            int page = 1;
                            for (Tablet tablet : tabletList) {
                                List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                                PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));

                                for (int i = 0; i < names.size(); i++) {
                                    switch (names.get(i).getType()) {
                                        case 2: {
                                            personYangShangFill(names.get(i).getText().split("@"), canvas, font,
                                                    336,
                                                    497,
                                                    23,
                                                    311,
                                                    520,
                                                    311,
                                                    425,
                                                    30,
                                                    20);
                                            break;
                                        }
                                        case 4: {
                                            // 地基主
                                            dijizhuFill(canvas, font, names.get(i).getText().split("@"), 425, 775, 400.0f, 390);
                                            break;
                                        }
                                    }
                                }
                                //TODO 编号
                            }

                        }

                    } else {
                        originPdf = new PdfDocument(new PdfReader("pdf_new_template/big/延生牌位-企业-大牌位.pdf"));
                        for (int i = 0; i < sum; i++) {
                            originPdf.copyPagesTo(1, 1, resultPdf);
                        }
                        int page = 1;
                        for (Tablet tablet : tabletList) {
                            List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                            PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                            for (int i = 0; i < names.size(); i++) {
                                if (names.get(i).getType() == 3) {
                                    companyYangShangFill(canvas, font, names.get(i).getText().split("@"),
                                            785,
                                            420,
                                            440,
                                            0);
                                    break;
                                }
                            }
                        }
                    }

                }
                break;
            }

            case 2: {
                if (flagTablet.getPrintType() == 1) {
                    //个人
                    if (WANGSHENG.equals(flagTablet.getTabletType())) {
                        if (WANGSHENG_LIDAIXIANZU.equals(flagTablet.getLastTabletType()) || WANGSHENG_XIANREN.equals(flagTablet.getLastTabletType())) {
                            originPdf = new PdfDocument(new PdfReader("pdf_new_template/middle/往生牌位-历代祖先-中牌位.pdf"));
                            for (int i = 0; i < sum; i++) {
                                originPdf.copyPagesTo(1, 1, resultPdf);
                            }
                            int page = 1;
                            for (Tablet tablet : tabletList) {
                                List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                                PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                                //祖先姓
                                zuXianXinFill(canvas, font, tablet.getAncestralSurname(), 28, 145, 625);
                                for (int i = 0; i < names.size(); i++) {
                                    switch (names.get(i).getType()) {
                                        case 1: {
                                            ancestralFill(canvas, font, names.get(i).getText().split("@"), 18, 192, 580);
                                            break;
                                        }
                                        case 2: {
                                            personYangShangFill(names.get(i).getText().split("@"), canvas, font,
                                                    104,
                                                    360,
                                                    19,
                                                    78,
                                                    380,
                                                    78,
                                                    318,
                                                    22,
                                                    18);
                                            break;
                                        }
                                    }
                                }

                                //TODO 编号部分
                            }


                        } else if (WANGSHENG_SHIFANG.equals(flagTablet.getLastTabletType())) {
                            originPdf = new PdfDocument(new PdfReader("pdf_new_template/middle/往生牌位-十方法界-中牌位.pdf"));
                            for (int i = 0; i < sum; i++) {
                                originPdf.copyPagesTo(1, 1, resultPdf);
                            }
                            int page = 1;
                            for (Tablet tablet : tabletList) {
                                List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                                PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                                for (int i = 0; i < names.size(); i++) {
                                    if (names.get(i).getType() == 2) {
                                        personYangShangFill(names.get(i).getText().split("@"), canvas, font,
                                                103,
                                                385,
                                                11,
                                                78,
                                                415,
                                                78,
                                                315,
                                                22, 18);
                                        break;
                                    }
                                }
                                //TODO 编号
                                //numberFill();
                            }
                        } else if (WANGSHENG_DIJIZHU.equals(flagTablet.getLastTabletType())) {
                            originPdf = new PdfDocument(new PdfReader("pdf_new_template/middle/往生牌位-地基主-中牌位.pdf"));
                            for (int i = 0; i < sum; i++) {
                                originPdf.copyPagesTo(1, 1, resultPdf);
                            }

                            int page = 1;
                            for (Tablet tablet : tabletList) {
                                List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                                PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));

                                for (int i = 0; i < names.size(); i++) {
                                    switch (names.get(i).getType()) {
                                        case 2: {
                                            personYangShangFill(names.get(i).getText().split("@"), canvas, font,
                                                    99,
                                                    425,
                                                    15,
                                                    82,
                                                    430,
                                                    82,
                                                    370,
                                                    25,
                                                    15);
                                            break;
                                        }
                                        case 4: {
                                            // 地基主
                                            dijizhuFill(canvas, font, names.get(i).getText().split("@"), 174, 600, 300.0f, 140);
                                            break;
                                        }
                                    }
                                }
                                //TODO 编号
                            }

                        }
                    } else {
                        originPdf = new PdfDocument(new PdfReader("pdf_new_template/middle/延生牌位-中牌位.pdf"));
                        for (int i = 0; i < sum; i++) {
                            originPdf.copyPagesTo(1, 1, resultPdf);
                        }
                        int page = 1;
                        for (Tablet tablet : tabletList) {
                            List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                            PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                            for (int i = 0; i < names.size(); i++) {
                                if (names.get(i).getType() == 3) {
                                    personYangShangFill(names.get(i).getText().split("@"), canvas, font,
                                            160,
                                            550,
                                            23,
                                            136,
                                            575,
                                            136,
                                            477,
                                            30,
                                            20);
                                    break;
                                }
                            }
                        }

                        //TODO 编号部分
                        //numberFill(canvas,font,tablet.getId(),255,375);
                    }
                } else {
                    if (WANGSHENG.equals(flagTablet.getTabletType())) {
                        if (WANGSHENG_SHIFANG.equals(flagTablet.getLastTabletType())) {
                            originPdf = new PdfDocument(new PdfReader("pdf_new_template/middle/往生牌位-十方法界-企业-中牌位.pdf"));
                            for (int i = 0; i < sum; i++) {
                                originPdf.copyPagesTo(1, 1, resultPdf);
                            }
                            int page = 1;
                            for (Tablet tablet : tabletList) {
                                List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                                PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                                for (int i = 0; i < names.size(); i++) {
                                    if (names.get(i).getType() == 2) {

                                        break;
                                    }
                                }
                                //TODO 编号
                                //numberFill();
                            }
                        } else if (WANGSHENG_DIJIZHU.equals(flagTablet.getLastTabletType())) {
                            originPdf = new PdfDocument(new PdfReader("pdf_new_template/middle/往生牌位-地基主-中牌位.pdf"));
                            for (int i = 0; i < sum; i++) {
                                originPdf.copyPagesTo(1, 1, resultPdf);
                            }

                            int page = 1;
                            for (Tablet tablet : tabletList) {
                                List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                                PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));

                                for (int i = 0; i < names.size(); i++) {
                                    switch (names.get(i).getType()) {
                                        case 2: {
                                            personYangShangFill(names.get(i).getText().split("@"), canvas, font, 99,
                                                    425,
                                                    15,
                                                    82,
                                                    430,
                                                    82,
                                                    370, 25, 15);
                                            break;
                                        }
                                        case 4: {
                                            // 地基主
                                            dijizhuFill(canvas, font, names.get(i).getText().split("@"), 174, 600, 300.0f, 140);
                                            break;
                                        }
                                    }
                                }
                                //TODO 编号
                            }

                        }

                    } else {
                        originPdf = new PdfDocument(new PdfReader("pdf_new_template/middle/延生牌位-企业-中牌位.pdf"));
                        for (int i = 0; i < sum; i++) {
                            originPdf.copyPagesTo(1, 1, resultPdf);
                        }
                        int page = 1;
                        for (Tablet tablet : tabletList) {
                            List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                            PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                            for (int i = 0; i < names.size(); i++) {
                                if (names.get(i).getType() == 3) {
                                    companyYangShangFill(canvas, font, names.get(i).getText().split("@"), 600, 175, 300, 0);
                                    break;
                                }
                            }
                        }
                    }

                }
                break;
            }
            case 3: {
                sum = (int) Math.ceil((double)sum / 4);
                if (WANGSHENG.equals(flagTablet.getTabletType())) {
                    if (WANGSHENG_LIDAIXIANZU.equals(flagTablet.getLastTabletType()) || WANGSHENG_XIANREN.equals(flagTablet.getLastTabletType())) {
                        originPdf = new PdfDocument(new PdfReader("pdf_new_template/little/祖先-小.pdf"));
                        for (int i = 0; i < sum; i++) {
                            originPdf.copyPagesTo(1, 1, resultPdf);
                        }
                        int page = 1;
                        int[] littleXianZu = new int[]{0, 207, 2 * 207, 3 * 207};
                        for (int i = 0; i < tabletList.size(); i += 4) {
                            PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                            for (int j = i; j < tabletList.size() && j < i + 4; j++) {
                                //祖先性
                                if (StringUtils.isNotEmpty(tabletList.get(j).getAncestralSurname())) {
                                    ancestralFill(canvas, font, new String[]{tabletList.get(j).getAncestralSurname()}, 8, 103 + littleXianZu[j - i], 337);
                                }
                                List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tabletList.get(j).getId()));
                                for (int k = 0; k < names.size(); k++) {
                                    //阳上
                                    if (names.get(k).getType() == 2) {
                                        personYangShangFill(names.get(k).getText().split("@"),
                                                canvas,
                                                font,
                                                62 + littleXianZu[k],
                                                231,
                                                8,
                                                50+ littleXianZu[k],
                                                245,
                                                50+ littleXianZu[k],
                                                200,
                                                16,
                                                10);
                                    }
                                }
                            }

                        }
                    } else if(WANGSHENG_SHIFANG.equals(flagTablet.getLastTabletType())){
                        originPdf = new PdfDocument(new PdfReader("pdf_new_template/little/十方-小.pdf"));
                        for (int i = 0; i < sum; i++) {
                            originPdf.copyPagesTo(1, 1, resultPdf);
                        }
                        int page = 1;
                        int[] littleShiFang = new int[]{0, 209, 2 * 209, 3 * 210};
                        for (int i = 0; i < tabletList.size(); i += 4) {
                            PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                            for (int j = i; j < tabletList.size() && j < i + 4; j++) {
                                List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tabletList.get(j).getId()));
                                for (int k = 0; k < names.size(); k++) {
                                    //阳上
                                    if (names.get(k).getType() == 2) {
                                        personYangShangFill(names.get(k).getText().split("@"),
                                                canvas,
                                                font,
                                                57 + littleShiFang[k],
                                                242,
                                                8,
                                                41+ littleShiFang[k],
                                                255,
                                                41+ littleShiFang[k],
                                                209,
                                                16,
                                                12);
                                    }
                                }
                            }

                        }
                    }else if(WANGSHENG_DIJIZHU.equals(flagTablet.getLastTabletType())){
                        originPdf = new PdfDocument(new PdfReader("pdf_new_template/little/地基主-小.pdf"));
                        for (int i = 0; i < sum; i++) {
                            originPdf.copyPagesTo(1, 1, resultPdf);
                        }
                        int page = 1;
                        int[] littleShiFang = new int[]{0, 209, 2 * 209, 3 * 210};
                        for (int i = 0; i < tabletList.size(); i += 4) {
                            PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                            for (int j = i; j < tabletList.size() && j < i + 4; j++) {
                                List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tabletList.get(j).getId()));
                                for (int k = 0; k < names.size(); k++) {
                                    //阳上
                                    if (names.get(k).getType() == 2) {
                                        personYangShangFill(names.get(k).getText().split("@"),
                                                canvas,
                                                font,
                                                52 + littleShiFang[k],
                                                244,
                                                8,
                                                35+ littleShiFang[k],
                                                255,
                                                35+ littleShiFang[k],
                                                209,
                                                16,
                                                12);
                                    } else if(names.get(k).getType() == 4){
                                        dijizhuFill(canvas,font,names.get(k).getText().split("@"),105+ littleShiFang[k],340,140,0);
                                    }
                                }
                            }

                        }
                    }
                } else {
                    //TODO 牌位位置
                    originPdf = new PdfDocument(new PdfReader("pdf_new_template/little/延生-小.pdf"));
                    for (int i = 0; i < sum; i++) {
                        originPdf.copyPagesTo(1, 1, resultPdf);
                    }
                    int page = 1;
                    int[] littleYanSheng = new int[]{107, 315, 523, 731};
                    for (int i = 0; i < tabletList.size(); i+=4) {
                        PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                        for (int j = i; j < tabletList.size() && j < i + 4; j++) {
                            List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tabletList.get(j).getId()));
                            for (int k = 0; k < names.size(); k++) {
                                if (names.get(k).getType() == 3) {
                                    littleNameFill(canvas, font, names.get(k).getText().split("@"), littleYanSheng[j - i], 333, 273);
                                }
                            }
                        }
                    }

                }
                break;
            }
            default: {
                throw new BaseException(400, "牌位大小错误，无法打印");
            }

        }
        if (originPdf != null) {
            originPdf.close();
        }
        resultPdf.close();
        response.getOutputStream().flush();
        response.getOutputStream().close();

        /*//更新牌位信息
        for(Tablet tablet : tabletList){
            Tablet updateTablet = new Tablet();
            updateTablet.setId(tablet.getId());
            updateTablet.setPrintStatus(2);
            tabletMapper.updateById(updateTablet);
        }*/

    }


    /**
     * 地基主填充
     */
    public void dijizhuFill(PdfCanvas canvas, PdfFont font, String[] addresses, double middle, double top, float height, double leftLimit) {
        if (addresses == null || addresses.length == 0) {
            return;
        }
        float max = 0;
        for (int i = 0; i < addresses.length; i++) {
            max = Math.max(max, addresses[i].length());
        }
        double xLocation;
        float size;
        if (addresses.length <= 3) {
            size = (float) Math.min((middle - leftLimit) / (addresses.length / 2.0), height / max * 1.0);
            xLocation = middle - size * addresses.length / 2.0;
        } else {
            size = height / (max < 6 ? 6 : max);
            xLocation = leftLimit;
        }
        for (int i = 0; i < addresses.length; i++) {
            double yLocation = top - (height - size * addresses[i].length()) / 2.0;
            canvas.beginText().setFontAndSize(font, size).setLeading(size).moveText(xLocation, yLocation);
            for (int j = 0; j < addresses[i].length(); j++) {
                canvas.newlineShowText(String.valueOf(addresses[i].charAt(j)));
            }
            xLocation += size;
        }
    }

    /**
     * 编号填充
     */
    public void numberFill(PdfCanvas canvas, PdfFont font, Long idNumber, int xLocation, int yLocation) {
        canvas.beginText()
                .setFontAndSize(font, 15)
                .setLeading(30 * 1.2f)
                .moveText(xLocation, yLocation);
        canvas.newlineShowText("编号:" + StringUtils.beDigits(idNumber));
        canvas.endText();
    }

    /**
     * 企业阳上填充方法
     */
    public void companyYangShangFill(PdfCanvas canvas, PdfFont font, String[] names, float top, double middle, float bottom, double rightLimit) {
        int height = 0, width = 0;

        if (names[names.length - 1].length() <= 8 || (names[names.length - 1].length() >= 15 && names[names.length - 1].length() <= 23)) {
            names[names.length - 1] = names[names.length - 1] + "全体员工及客户";
        } else {
            String[] temp = new String[names.length + 1];
            System.arraycopy(names, 0, temp, 0, names.length);
            temp[names.length] = "全体员工及客户";
            names = temp;
        }

        int[] nameWidth = new int[names.length];

        for (int i = 0; i < names.length; i++) {
            nameWidth[i] = (int) Math.ceil(names[i].length() / 15.0);
            width += nameWidth[i];
            height = Math.max(height, names[i].length());
        }
        if (height > 15) {
            height = 15;
        } else if (height < 5) {
            height = 8;
        }
        float size = (top - bottom) / height;
        //判断有没有超出边界
        double xLocation, yLocation;
        if (rightLimit > 0 && (middle - rightLimit) * 2 > size * width) {
            xLocation = rightLimit - size;
        } else {
            xLocation = middle + (width / 2.0 - 1) * size;
        }
        yLocation = (top + bottom + height * size) / 2.0;
        //输出
        for (int i = 0; i < names.length; i++) {

            for (int j = 0; j < nameWidth[i]; j++) {
                canvas.beginText().setFontAndSize(font, size).setLeading(size).moveText(xLocation, yLocation);
                for (int k = j * 15; k < (j + 1) * 15 && k < names[i].length(); k++) {
                    canvas.newlineShowText(String.valueOf(names[i].charAt(k)));
                }
                xLocation -= size;
                canvas.endText();
            }
        }

    }

    /**
     * 祖先姓氏填充方法
     */
    public static void zuXianXinFill(PdfCanvas canvas, PdfFont font, String sureName, float size, double xLocation, double yLocation) {
        if (sureName.length() == 2) {
            canvas.beginText().setFontAndSize(font, size).setLeading(size).moveText(xLocation, yLocation);
            canvas.newlineShowText(sureName);
        } else {
            canvas.beginText().setFontAndSize(font, size).setLeading(size).moveText(xLocation + size / 2.0, yLocation);
            canvas.newlineShowText(sureName);
        }
        canvas.endText();
    }

    /**
     * 先人填充方法
     */
    public void ancestralFill(PdfCanvas canvas, PdfFont font, String[] ancestors, float size, double xLocation1, double yLocation1) {
        if (ancestors.length == 1) {
            //460 740
            size *= 1.5f;
            canvas.beginText().setFontAndSize(font, size).setLeading(size).moveText(xLocation1, yLocation1);
            for (int j = 0; j < ancestors[0].length(); j++) {
                canvas.newlineShowText(String.valueOf(ancestors[0].charAt(j)));
            }
        } else if (ancestors.length > 1 && ancestors.length <= 6) {
            int flag = ancestors.length / 2;
            double yLocation;
            double oddAdd;
            if ((ancestors.length & 1) == 1) {
                oddAdd = (size + 2) * 2;
            } else {
                oddAdd = 0;
                flag--;
            }

            for (int i = 0; i < ancestors.length; i++) {
                if (i <= flag) {
                    //内测
                    yLocation = yLocation1 - i * 4 * (size + 2) - (4 - ancestors[i].length()) * size / 2.0;
                    canvas.beginText().setFontAndSize(font, size).setLeading(size).moveText(xLocation1, yLocation);
                    for (int j = 0; j < ancestors[i].length(); j++) {
                        canvas.newlineShowText(String.valueOf(ancestors[i].charAt(j)));
                    }
                } else {
                    //外侧
                    yLocation = yLocation1 - (i - flag - 1) * 4 * (size + 2) - (4 - ancestors[i].length()) * size / 2.0 - oddAdd;
                    canvas.beginText().setFontAndSize(font, size).setLeading(size).moveText(xLocation1 + size, yLocation);
                    for (int j = 0; j < ancestors[i].length(); j++) {
                        canvas.newlineShowText(String.valueOf(ancestors[i].charAt(j)));
                    }
                }
            }
        }
    }

    /**
     * 祖先姓一行填充方法
     */
    public void aLineSureNames(PdfCanvas canvas, PdfFont font, String[] sureNames, int begin, int end, double xLocation, double yLocation, float size) {
        for (int i = begin; i <= end; i++) {
            if (sureNames[i].length() == 2) {
                canvas.beginText().setFontAndSize(font, size).setLeading(35 * 1.0f).moveText(xLocation, yLocation);
                canvas.newlineShowText(sureNames[i]);
            } else if (sureNames[i].length() == 1) {
                canvas.beginText().setFontAndSize(font, size).setLeading(35 * 1.0f).moveText(xLocation + size / 2.0, yLocation);
                canvas.newlineShowText(sureNames[i]);
            } else {
                xLocation -= 2 * size;
            }
            xLocation += 2 * size + 4;
        }
    }

    /**
     * 小牌位阳上横向填充
     */
    public void littleNameFill(PdfCanvas canvas, PdfFont font, String[] names, double middle, double top, double bottom) {
        float size = (float) ((top - bottom) / 4.0);
        double xLocation = middle - names.length / 2.0f * size;
        for (int i = 0; i < names.length; i++) {
            double fourAdd = 0.0;
            if (names[i].length() == 2) {
                names[i] = names[i].charAt(0) + " " + names[i].charAt(1);
            } else if (names[i].length() == 4) {
                fourAdd = size / 2.0;
            }
            canvas.beginText().setFontAndSize(font, size).setLeading(size).moveText(xLocation + i * size, top + fourAdd);
            for (int j = 0; j < names[i].length(); j++) {
                canvas.newlineShowText(String.valueOf(names[i].charAt(j)));
            }
        }

    }

    /**
     * 个人阳上填充方法
     */
    public void personYangShangFill(String[] names,
                                    PdfCanvas canvas,
                                    PdfFont font,
                                    double xLocation1,
                                    double yLocation1,
                                    int yLocatin1Add,
                                    double xLocation2_1,
                                    double yLocation2_1,
                                    double xLcation2_2,
                                    double yLocation2_2,
                                    float size1,
                                    float size2
    ) {
        if (names.length == 1) {
            // 单个名字
            String name = names[0];
            if (name.length() == 2) {
                name = name.charAt(0) + " " + name.charAt(1);
            }

            if (name.length() == 4) {
                yLocation1 += yLocatin1Add;
            }
            canvas.beginText()
                    .setFontAndSize(font, size1)
                    .setLeading(size1 * 1.2f)
                    .moveText(xLocation1, yLocation1);
            for (int j = 0; j < name.length(); j++) {
                canvas.newlineShowText(String.valueOf(name.charAt(j)));
            }
        } else if (names.length < 9) {
            int flag = names.length / 2;
            double xLocation, yLocation;
            float size;
            for (int i = 0; i < names.length; i++) {
                size = size2;
                if (i >= flag) {
                    yLocation = yLocation2_1;
                    xLocation = xLocation2_1 + (4 - names.length / 2) * size / 2.0f + (i - names.length / 2.0) * size;
                } else {
                    yLocation = yLocation2_2;
                    xLocation = xLcation2_2 + (4 - names.length / 2) * size / 2.0f + i * size;
                }
                if (names[i].length() == 4) {
                    yLocation += 1;
                    xLocation += 2;
                    size = size2 * 4 / 5.0f;
                }
                canvas.beginText()
                        .setFontAndSize(font, size)
                        .setLeading(size * 1.1f)
                        .moveText(xLocation, yLocation);
                if (names[i].length() == 2) {
                    names[i] = names[i].charAt(0) + " " + names[i].charAt(1);
                }
                for (int j = 0; j < names[i].length(); j++) {
                    canvas.newlineShowText(String.valueOf(names[i].charAt(j)));
                }

            }
        }
    }

    @Override
    public void getTabletPaperByIdsV1(List<Long> ids, HttpServletResponse response) throws IOException {
        List<Tablet> tabletList = tabletMapper.selectBatchIds(ids);
        int sum = tabletList.size();
        Tablet flagTablet;
        if (sum > 0) {
            flagTablet = checkTabletInfo(tabletList);
        } else {
            throw new BaseException(400, "暂无相关id的牌位信息");
        }
        response.setContentType("application/octet-stream;charset=utf-8");
        PdfFont font = PdfFontFactory.createFont("pdf_template/STKAITI.TTF", "Identity-H");
        // TODO: 个人企业
        if (flagTablet.getSizeType() == 1) {
            if (YANSHENG.equals(flagTablet.getTabletType())) {
                PdfDocument originPdf = new PdfDocument(new PdfReader("pdf_template/yansheng_big.pdf"));
                PdfDocument resultPdf = new PdfDocument(new PdfWriter(response.getOutputStream()));
                for (int i = 0; i < sum; i++) {
                    originPdf.copyPagesTo(1, 1, resultPdf);
                }
                int page = 1;
                for (Tablet tablet : tabletList) {
                    List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                    PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                    if (names.size() == 1) {
                        // 单个名字
                        String name = names.get(0).getText();
                        if (name.length() == 2) {
                            name = name.charAt(0) + " " + name.charAt(1);
                        }
                        double yLocation = 750;
                        if (name.length() == 4) {
                            yLocation += 23;
                        }
                        canvas.beginText()
                                .setFontAndSize(font, 30)
                                .setLeading(30 * 1.2f)
                                .moveText(410, yLocation);
                        for (int j = 0; j < name.length(); j++) {
                            canvas.newlineShowText(String.valueOf(name.charAt(j)));
                        }
                    } else if (names.size() < 9) {
                        int flag = names.size() / 2;
                        double yLocation;
                        double xLocation;
                        float size;
                        for (int i = 0; i < names.size(); i++) {
                            if (i >= flag) {
                                yLocation = 770;
                                xLocation = 380 + (2 - names.size() / 4.0) * 25 + (i - names.size() / 2.0) * 25;
                            } else {
                                yLocation = 690;
                                xLocation = 380 + (2 - names.size() / 4.0) * 25 + i * 25;
                            }
                            if (names.get(i).getText().length() == 4) {
                                yLocation += 1;
                                xLocation += 2;
                                size = 16;
                            } else {
                                size = 20;
                            }
                            canvas.beginText()
                                    .setFontAndSize(font, size)
                                    .setLeading(size * 1.1f)
                                    .moveText(xLocation, yLocation);
                            if (names.get(i).getText().length() == 2) {
                                names.get(i).setText(names.get(i).getText().charAt(0) + " " + names.get(i).getText().charAt(1));
                            }
                            for (int j = 0; j < names.get(i).getText().length(); j++) {
                                canvas.newlineShowText(String.valueOf(names.get(i).getText().charAt(j)));
                            }
                        }
                    } else {
                        int lineSize = names.size() % 3 == 0 ? names.size() / 3 : names.size() / 3 + 1;
                        double yLocation;
                        double xLocation;
                        float leading;
                        float size;
                        for (int i = 0; i < 3; i++) {
                            for (int j = 0; j < lineSize; j++) {
                                if (i * lineSize + j == names.size()) {
                                    break;
                                }
                                yLocation = 770 - 55 * i;
                                xLocation = 370 + (4 - lineSize) * 10 + (lineSize - j) * 20;
                                if (names.get(i * lineSize + j).getText().length() == 4) {
                                    yLocation += 2;
                                    xLocation += 0.5;
                                    leading = 13 * 1.0f;
                                    size = 13;
                                } else {
                                    leading = 16 * 1.0f;
                                    size = 16;
                                }
                                canvas.beginText()
                                        .setFontAndSize(font, size)
                                        .setLeading(leading)
                                        .moveText(xLocation, yLocation);
                                if (names.get(i * lineSize + j).getText().length() == 2) {
                                    names.get(i * lineSize + j).setText(names.get(i * lineSize + j).getText().charAt(0) + " " + names.get(i * lineSize + j).getText().charAt(1));
                                }
                                for (int k = 0; k < names.get(i * lineSize + j).getText().length(); k++) {
                                    canvas.newlineShowText(String.valueOf(names.get(i * lineSize + j).getText().charAt(k)));
                                }
                            }
                        }
                    }
                    // 编号部分
                    canvas.beginText()
                            .setFontAndSize(font, 15)
                            .setLeading(30 * 1.2f)
                            .moveText(390, 290);
                    canvas.newlineShowText("编号:" + StringUtils.beDigits(tablet.getId()));
                    canvas.endText();
                    resultPdf.close();
                    response.getOutputStream().flush();
                    response.getOutputStream().close();
                }
            } else {
                if (WANGSHENG_DIJIZHU.equals(flagTablet.getLastTabletType())) {
                    PdfDocument originPdf = new PdfDocument(new PdfReader("pdf_template/wangsheng_dijizhu_big.pdf"));
                    PdfDocument resultPdf = new PdfDocument(new PdfWriter(response.getOutputStream()));
                    for (int i = 0; i < sum; i++) {
                        originPdf.copyPagesTo(1, 1, resultPdf);
                    }
                    int page = 1;
                    for (Tablet tablet : tabletList) {
                        List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                        PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                        String address = tablet.getFoundationAddress();
                        if (names.size() == 1) {
                            // 单个名字
                            String name = names.get(0).getText();
                            if (name.length() == 2) {
                                name = name.charAt(0) + " " + name.charAt(1);
                            }
                            double yLocation = 490;
                            if (name.length() == 4) {
                                yLocation = 513;
                            }
                            canvas.beginText()
                                    .setFontAndSize(font, 30)
                                    .setLeading(30 * 1.2f)
                                    .moveText(330, yLocation);
                            for (int j = 0; j < name.length(); j++) {
                                canvas.newlineShowText(String.valueOf(name.charAt(j)));
                            }
                        } else if (names.size() < 9) {
                            int flag = names.size() / 2;
                            double yLocation;
                            double xLocation;
                            float size;
                            for (int i = 0; i < names.size(); i++) {
                                if (i >= flag) {
                                    yLocation = 510;
                                    xLocation = 300 + (2 - names.size() / 4.0) * 25 + (i - names.size() / 2.0) * 25;
                                } else {
                                    yLocation = 430;
                                    xLocation = 300 + (2 - names.size() / 4.0) * 25 + i * 25;
                                }
                                if (names.get(i).getText().length() == 4) {
                                    yLocation += 1;
                                    xLocation += 2;
                                    size = 16;
                                } else {
                                    size = 20;
                                }
                                canvas.beginText()
                                        .setFontAndSize(font, size)
                                        .setLeading(size * 1.1f)
                                        .moveText(xLocation, yLocation);
                                if (names.get(i).getText().length() == 2) {
                                    names.get(i).setText(names.get(i).getText().charAt(0) + " " + names.get(i).getText().charAt(1));
                                }
                                for (int j = 0; j < names.get(i).getText().length(); j++) {
                                    canvas.newlineShowText(String.valueOf(names.get(i).getText().charAt(j)));
                                }
                            }
                        } else if (names.size() < 16) {
                            int lineSize = names.size() % 3 == 0 ? names.size() / 3 : names.size() / 3 + 1;
                            double yLocation;
                            double xLocation;
                            float leading;
                            float size;
                            for (int i = 0; i < 3; i++) {
                                for (int j = 0; j < lineSize; j++) {
                                    if (i * lineSize + j == names.size()) {
                                        break;
                                    }
                                    yLocation = 515 - 55 * i;
                                    xLocation = 290 + (4 - lineSize) * 10 + (lineSize - j) * 20;
                                    if (names.get(i * lineSize + j).getText().length() == 4) {
                                        yLocation += 2;
                                        xLocation += 0.5;
                                        size = 13;
                                        leading = size;
                                    } else {
                                        size = 16;
                                        leading = size;
                                    }
                                    canvas.beginText()
                                            .setFontAndSize(font, size)
                                            .setLeading(leading)
                                            .moveText(xLocation, yLocation);
                                    if (names.get(i * lineSize + j).getText().length() == 2) {
                                        names.get(i * lineSize + j).setText(names.get(i * lineSize + j).getText().charAt(0) + " " + names.get(i * lineSize + j).getText().charAt(1));
                                    }
                                    for (int k = 0; k < names.get(i * lineSize + j).getText().length(); k++) {
                                        canvas.newlineShowText(String.valueOf(names.get(i * lineSize + j).getText().charAt(k)));
                                    }
                                }
                            }
                        } else {
                            int lineSize = names.size() % 4 == 0 ? names.size() / 4 : names.size() / 4 + 1;
                            double yLocation;
                            double xLocation;
                            float leading;
                            float size;
                            for (int i = 0; i < 4; i++) {
                                for (int j = 0; j < lineSize; j++) {
                                    if (i * lineSize + j == names.size()) {
                                        break;
                                    }
                                    yLocation = 517 - 42 * i;
                                    xLocation = 290 + (4 - lineSize) * 10 + (lineSize - j) * 20;
                                    if (names.get(i * lineSize + j).getText().length() == 4) {
                                        yLocation += 2;
                                        xLocation += 0.5;
                                        size = 10;
                                        leading = size;
                                    } else {
                                        size = 12;
                                        leading = size;
                                    }
                                    canvas.beginText()
                                            .setFontAndSize(font, size)
                                            .setLeading(leading)
                                            .moveText(xLocation, yLocation);
                                    if (names.get(i * lineSize + j).getText().length() == 2) {
                                        names.get(i * lineSize + j).setText(names.get(i * lineSize + j).getText().charAt(0) + " " + names.get(i * lineSize + j).getText().charAt(1));
                                    }
                                    for (int k = 0; k < names.get(i * lineSize + j).getText().length(); k++) {
                                        canvas.newlineShowText(String.valueOf(names.get(i * lineSize + j).getText().charAt(k)));
                                    }
                                }
                            }
                        }
                        // 地基主地址部分
                        int addressSize = address.length();
                        int size = 25;
                        float xLocation = 425;
                        float yLocation = 775;
                        if (addressSize > 16) {
                            canvas.beginText()
                                    .setFontAndSize(font, size)
                                    .setLeading(size * 1.0f)
                                    .moveText(xLocation, yLocation);
                            for (int j = 0; j < 16; j++) {
                                canvas.newlineShowText(String.valueOf(address.charAt(j)));
                            }
                            canvas.beginText()
                                    .setFontAndSize(font, size)
                                    .setLeading(size * 1.0f)
                                    .moveText(xLocation - 25, yLocation);
                            for (int j = 16; j < (Math.min(addressSize, 32)); j++) {
                                canvas.newlineShowText(String.valueOf(address.charAt(j)));
                            }
                        } else {
                            canvas.beginText()
                                    .setFontAndSize(font, size)
                                    .setLeading(size * 1.0f)
                                    .moveText(xLocation - 15, yLocation);
                            for (int j = 0; j < addressSize; j++) {
                                canvas.newlineShowText(String.valueOf(address.charAt(j)));
                            }
                        }
                        // 编号部分
                        canvas.beginText()
                                .setFontAndSize(font, 15)
                                .setLeading(30 * 1.2f)
                                .moveText(380, 290);
                        canvas.newlineShowText("编号:" + StringUtils.beDigits(tablet.getId()));
                        canvas.endText();
                        resultPdf.close();
                    }
                } else if (WANGSHENG_LIDAIXIANZU.equals(flagTablet.getLastTabletType())) {
                    PdfDocument originPdf = new PdfDocument(new PdfReader("pdf_template/wangsheng_zuxian3_big.pdf"));
                    PdfDocument resultPdf = new PdfDocument(new PdfWriter(response.getOutputStream()));
                    for (int i = 0; i < sum; i++) {
                        originPdf.copyPagesTo(1, 1, resultPdf);
                    }
                    int page = 1;
                    for (Tablet tablet : tabletList) {
                        String ancestral = tablet.getAncestralSurname();
                        String sureName = "";
                        // todo:待定
                    }


                } else if (WANGSHENG_SHIFANG.equals(flagTablet.getLastTabletType())) {
                    PdfDocument originPdf = new PdfDocument(new PdfReader("pdf_template/wangsheng_shifang_big.pdf"));
                    PdfDocument resultPdf = new PdfDocument(new PdfWriter(response.getOutputStream()));
                    for (int i = 0; i < sum; i++) {
                        originPdf.copyPagesTo(1, 1, resultPdf);
                    }
                    int page = 1;
                    for (Tablet tablet : tabletList) {
                        List<Name> names = nameMapper.selectByMap(Map.of("tablet_id", tablet.getId()));
                        PdfCanvas canvas = new PdfCanvas(resultPdf.getPage(page++));
                        if (names.size() == 1) {
                            // 单个名字
                            String name = names.get(0).getText();
                            if (name.length() == 2) {
                                name = name.charAt(0) + " " + name.charAt(1);
                            }
                            double yLocation = 510;
                            if (name.length() == 4) {
                                yLocation += 23;
                            }
                            canvas.beginText()
                                    .setFontAndSize(font, 30)
                                    .setLeading(30 * 1.2f)
                                    .moveText(335, yLocation);
                            for (int j = 0; j < name.length(); j++) {
                                canvas.newlineShowText(String.valueOf(name.charAt(j)));
                            }
                        } else if (names.size() < 9) {
                            int flag = names.size() / 2;
                            double yLocation;
                            double xLocation;
                            float size;
                            for (int i = 0; i < names.size(); i++) {
                                if (i >= flag) {
                                    yLocation = 520;
                                    xLocation = 300 + (2 - names.size() / 4.0) * 25 + (i - names.size() / 2.0) * 25;
                                } else {
                                    yLocation = 445;
                                    xLocation = 300 + (2 - names.size() / 4.0) * 25 + i * 25;
                                }
                                if (names.get(i).getText().length() == 4) {
                                    yLocation += 1;
                                    xLocation += 2;
                                    size = 16;
                                } else {
                                    size = 20;
                                }
                                canvas.beginText()
                                        .setFontAndSize(font, size)
                                        .setLeading(size * 1.1f)
                                        .moveText(xLocation, yLocation);
                                if (names.get(i).getText().length() == 2) {
                                    names.get(i).setText(names.get(i).getText().charAt(0) + " " + names.get(i).getText().charAt(1));
                                }
                                for (int j = 0; j < names.get(i).getText().length(); j++) {
                                    canvas.newlineShowText(String.valueOf(names.get(i).getText().charAt(j)));
                                }
                            }
                        } else {
                            int lineSize = names.size() % 3 == 0 ? names.size() / 3 : names.size() / 3 + 1;
                            double yLocation;
                            double xLocation;
                            float leading;
                            float size;
                            for (int i = 0; i < 3; i++) {
                                for (int j = 0; j < lineSize; j++) {
                                    if (i * lineSize + j == names.size()) {
                                        break;
                                    }
                                    yLocation = 518 - 48 * i;
                                    xLocation = 300 + (4 - lineSize) * 10 + (lineSize - j) * 17;
                                    if (names.get(i * lineSize + j).getText().length() == 4) {
                                        yLocation += 0.5;
                                        xLocation += 0.5;
                                        size = 11;
                                        leading = size;
                                    } else {
                                        size = 14;
                                        leading = size;
                                    }
                                    canvas.beginText()
                                            .setFontAndSize(font, size)
                                            .setLeading(leading)
                                            .moveText(xLocation, yLocation);
                                    if (names.get(i * lineSize + j).getText().length() == 2) {
                                        names.get(i * lineSize + j).setText(names.get(i * lineSize + j).getText().charAt(0) + " " + names.get(i * lineSize + j).getText().charAt(1));
                                    }
                                    for (int k = 0; k < names.get(i * lineSize + j).getText().length(); k++) {
                                        canvas.newlineShowText(String.valueOf(names.get(i * lineSize + j).getText().charAt(k)));
                                    }
                                }
                            }
                        }
                        canvas.beginText()
                                .setFontAndSize(font, 15)
                                .setLeading(30 * 1.2f)
                                .moveText(380, 290);
                        canvas.newlineShowText("编号:" + StringUtils.beDigits(tablet.getId()));
                        canvas.endText();
                        resultPdf.close();
                    }
                }
            }

        }
    }

}

